import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;

import org.apache.commons.codec.binary.Base64;

public class Encryptor {
	
	 String encryption1 = " ";
    public static String encrypt(String key1, String key2, String value) {
        try {
            IvParameterSpec iv = new IvParameterSpec(key2.getBytes("UTF-8"));

            SecretKeySpec skeySpec = new SecretKeySpec(key1.getBytes("UTF-8"), "AES");
            
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
            
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
            
            byte[] encrypted = cipher.doFinal(value.getBytes());
      //      System.out.println("encrypted string:"
        //            + Base64.encodeBase64String(encrypted));
            return Base64.encodeBase64String(encrypted);
        
        } catch (Exception ex) {
            ex.printStackTrace();
        
        }
        
        return null;
    }

    public static String decrypt(String key1, String key2, String encrypted) {
        try {
            IvParameterSpec iv = new IvParameterSpec(key2.getBytes("UTF-8"));

            SecretKeySpec skeySpec = new SecretKeySpec(key1.getBytes("UTF-8"),"AES");
            
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
            
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
            
            byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted));

            return new String(original);
        
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        
        return null;
    }

    public static void main(String[] args) {
    	
    	String key_a = "Bar12345Bar12345"; // 128 bit key
        String key_b = "ThisIsASecretKey";
   		
   		// String encryption2 = "";
        int choice;
    			try{
    	
    				BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    				
    			     	System.out.println("Press 1 to Begin!");       
    				do
    				{

    	             	choice = Integer.parseInt(br.readLine());
    						
    						switch(choice)
    						{
    						
    						case 1:
    							System.out.println("Enter the Username " );
    	                        Scanner scanner1 = new Scanner(System.in);
    	                        String Username = scanner1.next();
    	                     String encryption1 =   encrypt(key_a, key_b, Username);
    	                     System.out.println("Encrypted Username"+"  "+encryption1);
    	                     System.out.println("Press 2 to Enter the Password " );
    	                     
    	                     break;
    							
    						case 2:
    	                        Scanner scanner2 = new Scanner(System.in);
    	                        String Password = scanner2.next();
    	                        String encryption2 =    encrypt(key_a, key_b, Password);
    	                        
    	                        System.out.println("Encrypted Password"+"  "+encryption2);
    	                        System.out.println("Press 3 to Enter exit!");
    	                        break;
    											
    						case 3: 
    							
    							System.exit(0);
    							break;
    						case 4:
    							String d = decrypt(key_a,key_b,encryption1);
    							break;
    						default:
    							System.out.println("Invalid selection :");
    							break;
    						}
    						
    					} while(choice != 5);
    			}
    			
    			catch (Exception ex){
    				ex.printStackTrace();
    			}
    			
    		}
    	
        // System.out.println(decrypt(key1, key2,
           //      encrypt(key1, key2, "Hello World")));
    }
